package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
//@Transactional
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<Destination> list = repository.findByIdIn(region.getRefIds());

        return list;
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();

        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }/*else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }*/
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(),
                Sort.Direction.ASC,"_id");

        System.err.println(query);

        return DBHelper.query(query,template,Destination.class,pageable,qo);
    }

    @Override
    public List<Destination> queryToasts(String destId) {
        List<Destination> list = new ArrayList<>();
        createToasts(list,destId);
        //list元素集合顺序调转
        Collections.reverse(list);
        return list;
    }
    //根>>中国>>广东>>广州
    private void createToasts(List<Destination> list,String parentId){
        //判断parentId是否有值
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        //根据id,查询到destination对象
        Destination dest = this.get(parentId);
        list.add(dest);
        //再查询dest对象是否有parentId
        if (StringUtils.hasLength(dest.getParentId())){
            //递归方式,继续执行创建toasts,直到parentId为空.
            this.createToasts(list,dest.getParentId());
        }
    }

    @Override
    public void updateInfo(String id, String info) {
        //部分字段更新
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("info",info);
        template.updateMulti(query,update,Destination.class);
    }

    @Override
    public List<Destination> queryDestByHotRegionId(String rid) {

        List<Destination> list = null;

        if("-1".equals(rid)){
            //查询国内所有省份
            list = repository.findByParentName("中国");
        }else {
            list = this.getDestByRegionId(rid);
        }
        //遍历地区下的子地区
        for (Destination dest : list){
            //只列出前五个子地区,按顺序排列
            Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> children = repository.findByParentId(dest.getId(),pageable);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Destination> getDestByDeep(int deep) {
        //System.err.println(repository.findByDeep(deep));
        return repository.findByDeep(deep);
    }


}
